#include  <msp430x14x.h>
#include <string.h>
#include "packet.h"
#include "TI_CC2500.h"

//initialize variables
char srcAddr;
char destAddr;
char user[20];

//contructor
void initPacket(char source, char destination, char username[20]){
  srcAddr = source;
  destAddr = destination;
  strncpy(user, username, 20);
}//end initPacket


/*************MUTATOR METHODS***************/
void setUsername(char username[20]){
  strncpy(user, username, 20);

}//end setUsername

void setDestAddr(char destination){
  destAddr = destination;
}//end setDestAddr

void setSrcAddr(char source){
  srcAddr = source;
}//end setSrcAddr

/************END MUTATORS METHODS************/

//sending a packet to wherever you want
void sendPacket(PACKET *packet){
  int i;
  RFSendPacket(((char *)packet), (packet->length+1));            // Send value over RF
  P2IFG &= ~0x10;                            // After pkt TX, this flag is set.
}//end packet


//takes a receive and stores it as a packet
PACKET receivePacket(char buffer[64], char len){
  PACKET packet;
  int i;
  packet.length = len;
  packet.dest = buffer[0];
  packet.src = buffer[1];
  packet.type = buffer[2];
  for(i=0;i<len-3;i++)
    packet.body[i] = buffer[i+3];

  return packet;
}//end receivePacket

//creates a guess packet
void createGuessPacket(PACKET *packet, int guessCount, char guessValue[4]){
  int i;
  packet->length = 0x09;
  packet->dest = destAddr;
  packet->src = srcAddr;
  packet->type = TYPE_GUESS;
  packet->body[0] = 0x00FF&(guessCount>>8);
  packet->body[1] = 0x00FF&guessCount;
  for(i=0;i<4;i++){
    packet->body[i+2] = guessValue[i];
  }//end for
}//end 

//create a reply packet
void createReplyPacket(PACKET *packet, char bulls, char cows){
  packet->length = 0x05;
  packet->dest = destAddr;
  packet->src = srcAddr;
  packet->type = TYPE_REPLY;
  packet->body[0] = bulls;
  packet->body[1] = cows;
}//end

//create a reply packet
void createServerPacket(PACKET *packet, char serverExecution, char *msg){
  int i;
  int msgLen = strlen(msg)+1;
  packet->length = (char)(msgLen + 5);
  packet->dest = 0xFF;
  packet->src = srcAddr;  
  packet->type = TYPE_SERVER;
  packet->body[0] = serverExecution; //set 'T' for termination
  for(i=0;i<msgLen;i++){
    packet->body[i+1] = msg[i];
  }//end for
}//end

//create a heartbeat packet
void createHeartbeatPacket(PACKET *packet, char num){
  packet->length = 0x04;
  packet->dest = destAddr;
  packet->src = srcAddr;
  packet->type = TYPE_HEARTBEAT;
  packet->body[0] = num;
}//end

//create a heartbeat packet
void createHostbeaconPacket(PACKET *packet){
  int i;
  int nameLen = strlen(user)+1;
  packet->length = (char)(nameLen + 3);
  packet->dest = 0xFF;
  packet->src = srcAddr;
  packet->type = TYPE_HOSTGAME;
  for(i=0;i<nameLen;i++){
    packet->body[i] = user[i];
  }//end for
}//end

//create a heartbeat packet
void createClientPacket(PACKET *packet){
  int i;
  int nameLen = strlen(user)+1; //for null
  packet->length = (char)(nameLen + 3);
  packet->dest = destAddr;
  packet->src = srcAddr;
  packet->type = TYPE_JOINGAME;
  for(i=0;i<nameLen;i++){
    packet->body[i] = user[i];
  }//end for
}//end
